//
//  RunSketchAppDelegate.m
//  RunSketch
//
//  Created by Brian on 10/1/09.
//  Copyright Tellart 2009. All rights reserved.
//

#import <MediaPlayer/MediaPlayer.h>
#import "RunSketchAppDelegate.h"
#import "MainViewController.h"
#import <AudioToolbox/AudioServices.h>

#define OVERRIDE_AUDIO_CATEGORY_MIX_WITH_OTHERS 1
//#define kAudioSessionCategory kAudioSessionCategory_PlayAndRecord
#define kAudioSessionCategory kAudioSessionCategory_MediaPlayback

@implementation RunSketchAppDelegate


@synthesize window;
@synthesize baseViewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    //hide the status bar (make full screen)
    [application setStatusBarHidden:YES];
    
	// Turn off the idle timer, since this app doesn't rely on constant touch input
	application.idleTimerDisabled = YES;
    
    [self initializeAudio];
    
    
    /*
     * set up the camera controller
     */
	CameraBaseViewController *aController = [[CameraBaseViewController alloc] initWithNibName:@"CameraBaseView" bundle:nil];
	self.baseViewController = aController;
	[aController release];
	
    baseViewController.view.frame = [UIScreen mainScreen].applicationFrame;
    
	[window addSubview:[baseViewController view]];
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [baseViewController release];
    [window release];
    [super dealloc];
}

- (void) initializeAudio
{
    OSStatus status;

    // Initialize and configure the audio session
    if (status = AudioSessionInitialize(NULL, NULL, audioSessionInterruptionListener, self)) {
        NSLog(@"RunSketchAppDelegate: ERROR: couldn't initialize audio session %i", status);
    }

    //advertise to the OS that we will be (potentially) both playing and recording audio
    UInt32 audioCategory = kAudioSessionCategory;
    if (status = AudioSessionSetProperty( kAudioSessionProperty_AudioCategory,
        sizeof(audioCategory), &audioCategory)) {
        NSLog(@"RunSketchAppDelegate: ERROR: couldn't set audio caregory to %s. Error code = %i", audioCategory, status);
    }
    
    //allow audio in the app to mix with other audio
    #if OVERRIDE_AUDIO_CATEGORY_MIX_WITH_OTHERS
    UInt32 doSetProperty = 1;
    if (status = AudioSessionSetProperty( kAudioSessionProperty_OverrideCategoryMixWithOthers,
        sizeof(doSetProperty), &doSetProperty)) {
        NSLog(@"RunSketchAppDelegate: ERROR: couldn't set kAudioSessionProperty_OverrideCategoryMixWithOthers to %i. Error code = %i", doSetProperty, status);
    }
    #endif
    
    #if __IPHONE_3_1
    //cause the audio to run through the speaker instead of the receiver
    UInt32 doChangeDefaultRoute = 1;
    if (status = AudioSessionSetProperty( kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
        sizeof(doChangeDefaultRoute), &doChangeDefaultRoute)) {
        NSLog(@"RunSketchAppDelegate: ERROR: couldn't set kAudioSessionProperty_OverrideCategoryDefaultToSpeaker to %i. Error code = %i", doChangeDefaultRoute, status);
    } else {
        NSLog(@"RunSketchAppDelegate: successfully set kAudioSessionProperty_OverrideCategoryDefaultToSpeaker to %i", doChangeDefaultRoute);
    }
    #endif
    
}

static void audioSessionInterruptionListener(void *inClientData, UInt32 inInterruption)
{
	printf("\nRunSketchAppDelegate: Session interrupted! --- %s ---", inInterruption == kAudioSessionBeginInterruption ? "Begin Interruption\n" : "End Interruption\n");
    
    if (!initialized) return;
	
	//NewAMIO *theNewAMIO = (NewAMIO*)inClientData;
	
	if (inInterruption == kAudioSessionEndInterruption) {
        //RunSketchAppDelegate *THIS = (RunSketchAppDelegate *)inClientData;
        
        //TODO
        /*
		// make sure we are again the active session
		AudioSessionSetActive(true);
        
        [theNewAMIO _setupAV];
        [theNewAMIO _setupAU];
        
        [theNewAMIO.carrierSampleFile reset];
    
        //AudioOutputUnitStart(outputAudioUnit);
        */
	}
	
	if (inInterruption == kAudioSessionBeginInterruption) {
    
        //TODO
        /*
		AudioOutputUnitStop(outputAudioUnit);
        */
    }
}


@end
